// <editor-fold defaultstate="collapsed" desc="GNU GPLv3 Header">
/*
 * File:   ChunkFormat.c
 *
 * Copyright (C) 2013 Robert Antoni Buj Gelonch <rbuj@uoc.edu>
 * Copyright (C) 2013 David Megias Jimenez <dmegias@uoc.edu>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
// </editor-fold>

#include "../ChunkFormat.h"

/**
 *
 * @param file
 * @return
 */
ChunkFormat* readChunkFormat(FILE* file) {
    ChunkFormat* chunkFormat = malloc(sizeof (ChunkFormat));
    chunkFormat->ck = readCk(file);
    if (strcmp(chunkFormat->ck->ID, "fmt ") != 0) {
        fprintf(stderr, "ckID != 'fmt '\n");
        exit(EXIT_FAILURE);
    }
    readWFormatTag(file, &(chunkFormat->wFormatTag));
    readNChannels(file, &(chunkFormat->nChannels));
    readNSamplesPerSec(file, &(chunkFormat->nSamplesPerSec));
    readNAvgBytesPerSec(file, &(chunkFormat->nAvgBytesPerSec));
    readNBlockAlign(file, &(chunkFormat->nBlockAlign));
    readWBitsPerSample(file, &(chunkFormat->wBitsPerSample));
    return chunkFormat;
}

/**
 *
 * @param file
 * @param wFormatTag
 */
void readWFormatTag(FILE* file, short int* wFormatTag) {
    readShortLE(file, wFormatTag);
}

/**
 *
 * @param file
 * @param nChannels
 */
void readNChannels(FILE* file, short int* nChannels) {
    readShortLE(file, nChannels);
}

/**
 *
 * @param file
 * @param nSamplesPerSec
 */
void readNSamplesPerSec(FILE* file, long int* nSamplesPerSec) {
    readLongLE(file, nSamplesPerSec);
}

/**
 *
 * @param file
 * @param nAvgBytesPerSec
 */
void readNAvgBytesPerSec(FILE* file, long int* nAvgBytesPerSec) {
    readLongLE(file, nAvgBytesPerSec);
}

/**
 *
 * @param file
 * @param nBlockAlign
 */
void readNBlockAlign(FILE* file, short int* nBlockAlign) {
    readShortLE(file, nBlockAlign);
}

/**
 *
 * @param file
 * @param wBitsPerSample
 */
void readWBitsPerSample(FILE* file, short int* wBitsPerSample) {
    readShortLE(file, wBitsPerSample);
}

/**
 *
 * @param chunkFormat
 */
void freeChunkFormat(ChunkFormat* chunkFormat) {
    freeCk(chunkFormat->ck);
    free(chunkFormat);
}

/**
 *
 * @param chunkFormat
 * @param file
 */
void writeChunkFormat(ChunkFormat* chunkFormat, FILE* file) {
    writeCk(chunkFormat->ck, file);
    writeWFormatTag(&(chunkFormat->wFormatTag), file);
    writeNChannels(&(chunkFormat->nChannels), file);
    writeNSamplesPerSec(&(chunkFormat->nSamplesPerSec), file);
    writeNAvgBytesPerSec(&(chunkFormat->nAvgBytesPerSec), file);
    writeNBlockAlign(&(chunkFormat->nBlockAlign), file);
    writeWBitsPerSample(&(chunkFormat->wBitsPerSample), file);
}

/**
 *
 * @param wFormatTag
 * @param file
 */
void writeWFormatTag(short int* wFormatTag, FILE* file) {
    writeShortLE(wFormatTag, file);
}

/**
 *
 * @param nChannels
 * @param file
 */
void writeNChannels(short int* nChannels, FILE* file) {
    writeShortLE(nChannels, file);
}

/**
 *
 * @param nSamplesPerSec
 * @param file
 */
void writeNSamplesPerSec(long int* nSamplesPerSec, FILE* file) {
    writeLongLE(nSamplesPerSec, file);
}

/**
 *
 * @param nAvgBytesPerSec
 * @param file
 */
void writeNAvgBytesPerSec(long int* nAvgBytesPerSec, FILE* file) {
    writeLongLE(nAvgBytesPerSec, file);
}

/**
 *
 * @param nBlockAlign
 * @param file
 */
void writeNBlockAlign(short int* nBlockAlign, FILE* file) {
    writeShortLE(nBlockAlign, file);
}

/**
 * 
 * @param wBitsPerSample
 * @param file
 */
void writeWBitsPerSample(short int* wBitsPerSample, FILE* file) {
    writeShortLE(wBitsPerSample, file);
}
